IF EXISTS ( SELECT 'X' FROM sys.procedures WHERE name = 'InsEventLog' AND schema_id = 1 )
BEGIN
     DROP PROCEDURE dbo.InsEventLog
END
GO

CREATE PROCEDURE dbo.InsEventLog
     @EventLogTypeId    int              = 0, -- Unknown
     @LogByApp          varchar(128)     = NULL,
     @LogByAppSubSystem varchar(128)     = NULL,
     @LogByDesc         varchar(128)     = NULL,
     @LogText           varchar(6144)    = NULL,
     @TraceText         varchar(max)     = NULL,
     @TraceImage        varbinary(max)   = NULL,
     @SID               varbinary(85)    = NULL,
     @MiscVC1           varchar(128)     = NULL,
     @MiscVC2           varchar(128)     = NULL,
     @MiscVC3           varchar(128)     = NULL,
     @MiscVC4           varchar(128)     = NULL,
     @MiscInt1          int              = NULL,
     @MiscInt2          int              = NULL,
     @MiscInt3          int              = NULL,
     @MiscInt4          int              = NULL,
     @MiscTinyInt1      tinyint          = NULL,
     @MiscTinyInt2      tinyint          = NULL,
     @MiscTinyInt3      tinyint          = NULL,
     @MiscTinyInt4      tinyint          = NULL,
     @MiscBigInt1       bigint           = NULL,
     @MiscBigInt2       bigint           = NULL,
     @MiscBigInt3       bigint           = NULL,
     @MiscBigInt4       bigint           = NULL,
     @MiscDt1           datetime         = NULL,
     @MiscDt2           datetime         = NULL,
     @MiscDt3           datetime         = NULL,
     @MiscDt4           datetime         = NULL,
     @MiscGUID1         uniqueidentifier = NULL,
     @MiscGUID2         uniqueidentifier = NULL,
     @MiscGUID3         uniqueidentifier = NULL,
     @MiscGUID4         uniqueidentifier = NULL
AS
/*
 *
 * InsEventLog
 *
 * Purpose:      
 *
 * Inputs:       
 *
 * Assumes:      
 *
 * Effects:      
 *
 * Returns:      
 *
 * Calling
 * Application:  
 *
 * Note(s):      
 *
 * Author(s):    NOVA Concepts Ltd.
 *
 * Change Log:   Initial Release, 05/29/2009, 1.00.0000
 *
 */
BEGIN
     SET XACT_ABORT ON
     SET NOCOUNT ON

     IF NOT EXISTS ( SELECT 'X' 
                     FROM   dbo.EventLogType
                     WHERE  EventLogTypeId = @EventLogTypeId )
     BEGIN -- Force it to unknown, we'd rather get the event than lose it.
          SELECT @EventLogTypeId = 0 
     END
     
     INSERT dbo.EventLog
          ( EventLogTypeId,
            LogByApp,
            LogByAppSubSystem,
            LogByDesc,
            LogText,
            TraceText,
            TraceImage,
            SID,
            MiscVC1,
            MiscVC2,
            MiscVC3,
            MiscVC4,
            MiscInt1,
            MiscInt2,
            MiscInt3,
            MiscInt4,
            MiscTinyInt1,
            MiscTinyInt2,
            MiscTinyInt3,
            MiscTinyInt4,
            MiscBigInt1,
            MiscBigInt2,
            MiscBigInt3,
            MiscBigInt4,
            MiscDt1,
            MiscDt2,
            MiscDt3,
            MiscDt4,
            MiscGUID1,
            MiscGUID2,
            MiscGUID3,
            MiscGUID4 )
     SELECT @EventLogTypeId,
            @LogByApp,
            @LogByAppSubSystem,
            @LogByDesc,
            @LogText,
            @TraceText,
            @TraceImage,
            @SID,
            @MiscVC1,
            @MiscVC2,
            @MiscVC3,
            @MiscVC4,
            @MiscInt1,
            @MiscInt2,
            @MiscInt3,
            @MiscInt4,
            @MiscTinyInt1,
            @MiscTinyInt2,
            @MiscTinyInt3,
            @MiscTinyInt4,
            @MiscBigInt1,
            @MiscBigInt2,
            @MiscBigInt3,
            @MiscBigInt4,
            @MiscDt1,
            @MiscDt2,
            @MiscDt3,
            @MiscDt4,
            @MiscGUID1,
            @MiscGUID2,
            @MiscGUID3,
            @MiscGUID4
END
GO

GRANT EXEC ON dbo.InsEventLog TO guest
GO

EXEC sp_addextendedproperty @name = N'MS_Description', @value = 'Inserts an event entry into the EventLog table.', @level0type = N'Schema', @level0name = dbo, @level1type = N'PROCEDURE', @level1name = InsEventLog;
GO
